package com.yuanmei.charts;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spanned;

import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yuanmei.views.R;
import com.yuanmei.common.Utils;

import java.util.ArrayList;
import java.util.List;

public class VolumeChartDataSet implements TaChartData {
    private List<BarEntry> mVolumeBarList;
    private List<Entry> mSma5VolumeList;
    private List<Entry> mSma10VolumeList;
    private List<Entry> mSma20VolumeList;
    private List<Entry> mSma30VolumeList;
    private List<Entry> mSma60VolumeList;
    private List<String> mXAxisDates;
    private CombinedData mVolumeCombineData;
    private Context mContext;
    private KLineChartDataSet mRawDataSet;

    public VolumeChartDataSet(Context context, KLineChartDataSet rawDataSet) {
        mContext = context;
        mRawDataSet = rawDataSet;
        mSma5VolumeList = new ArrayList<>();
        mSma10VolumeList = new ArrayList<>();
        mSma20VolumeList = new ArrayList<>();
        mSma30VolumeList = new ArrayList<>();
        mSma60VolumeList = new ArrayList<>();

        mVolumeBarList = new ArrayList<>();
        mXAxisDates = new ArrayList<>();

        List<Integer> colors = new ArrayList<Integer>();
        int green = Color.rgb(110, 190, 102);
        int red = Color.rgb(211, 74, 88);

        float sma5Volume = 0.0f, sma10Volume = 0.0f, sma20Volume = 0.0f, sma30Volume = 0.0f, sma60Volume = 0.0f;
        for (int i = 0; i < mRawDataSet.getXAxisSize(); i++) {
            mXAxisDates.add(mRawDataSet.getXAxisLabel(i));
            float volume = mRawDataSet.getVolume(i);
            mVolumeBarList.add(new BarEntry(i, volume));

            sma5Volume = sma5Volume + volume - ((i < 5) ? 0 : mRawDataSet.getVolume(i - 5));
            if ((i + 1) >= 5)
                mSma5VolumeList.add(new Entry(i, sma5Volume / 5));
            else
                mSma5VolumeList.add(new Entry(i, sma5Volume / (i + 1)));

            sma10Volume = sma10Volume + volume - ((i < 10) ? 0 : mRawDataSet.getVolume(i - 10));
            if ((i + 1) >= 10)
                mSma10VolumeList.add(new Entry(i, sma10Volume / 10));
            else
                mSma10VolumeList.add(new Entry(i, sma10Volume / (i + 1)));


            sma20Volume = sma20Volume + volume - ((i < 20) ? 0 : mRawDataSet.getVolume(i - 20));
            if ((i + 1) >= 20)
                mSma20VolumeList.add(new Entry(i, sma20Volume / 20));
            else
                mSma20VolumeList.add(new Entry(i, sma20Volume / (i + 1)));


            sma30Volume = sma30Volume + volume - ((i < 30) ? 0 : mRawDataSet.getVolume(i - 30));
            if ((i + 1) >= 30)
                mSma30VolumeList.add(new Entry(i, sma30Volume / 30));
            else
                mSma30VolumeList.add(new Entry(i, sma30Volume / (i + 1)));

            sma60Volume = sma60Volume + volume - ((i < 60) ? 0 : mRawDataSet.getVolume(i - 60));
            if ((i + 1) >= 60)
                mSma60VolumeList.add(new Entry(i, sma60Volume / 60));
            else
                mSma60VolumeList.add(new Entry(i, sma60Volume / (i + 1)));

            float change = mRawDataSet.getClose(i)  - mRawDataSet.getOpen(i);
            if (change >= 0)
                colors.add(red);
            else
                colors.add(green);
        }

        BarDataSet barSet = new BarDataSet(mVolumeBarList, "成交量");
        barSet.setHighlightEnabled(true);
        barSet.setHighLightAlpha(255);
        barSet.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        barSet.setDrawValues(false);

        barSet.setColors(colors);
        barSet.setValueTextColors(colors);
        BarData barData = new BarData(barSet);

        ArrayList<ILineDataSet> volSmaSet = new ArrayList<>();

        if (mSma5VolumeList.size() > 0) {
            LineDataSet vma5Set = new LineDataSet(mSma5VolumeList, "VMA5");
            TaLib.setCommonMaStyle(vma5Set);
            vma5Set.setHighlightEnabled(true);
            vma5Set.setDrawHorizontalHighlightIndicator(true);
            vma5Set.setDrawVerticalHighlightIndicator(true);
            vma5Set.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
            vma5Set.setColor(mContext.getResources().getColor(R.color.ma5));
            volSmaSet.add(vma5Set);
        }

        if (mSma10VolumeList.size() > 0) {
            LineDataSet vma10Set = new LineDataSet(mSma10VolumeList, "VMA10");
            TaLib.setCommonMaStyle(vma10Set);
            vma10Set.setColor(mContext.getResources().getColor(R.color.ma10));
            volSmaSet.add(vma10Set);
        }

        if (mSma20VolumeList.size() > 0) {
            LineDataSet vma20Set = new LineDataSet(mSma20VolumeList, "VMA20");
            TaLib.setCommonMaStyle(vma20Set);
            vma20Set.setColor(mContext.getResources().getColor(R.color.ma20));
            volSmaSet.add(vma20Set);
        }

        if (mSma30VolumeList.size() > 0) {
            LineDataSet vma30Set = new LineDataSet(mSma30VolumeList, "VMA30");
            TaLib.setCommonMaStyle(vma30Set);
            vma30Set.setColor(mContext.getResources().getColor(R.color.ma30));
            volSmaSet.add(vma30Set);
        }

        if (mSma60VolumeList.size() > 0) {
            LineDataSet vma60Set = new LineDataSet(mSma60VolumeList, "VMA60");
            TaLib.setCommonMaStyle(vma60Set);
            vma60Set.setColor(mContext.getResources().getColor(R.color.ma60));
            volSmaSet.add(vma60Set);
        }

        LineData smaLinesData = new LineData(volSmaSet);

        mVolumeCombineData = new CombinedData();
        mVolumeCombineData.setData(barData);
        mVolumeCombineData.setData(smaLinesData);
    }

    @Override
    public int getXAxisSize() {
        return mXAxisDates.size();
    }

    @Override
    public Spanned getDescription(int index) {
        String vol = "";
        if (index >= 0 && index < mVolumeBarList.size()) {
            vol = Utils.volumeToShortUnit(mVolumeBarList.get(index).getY());
        }

        String vma5 = "";
        if (index >= 0 && index < mSma5VolumeList.size()) {
            vma5 = Utils.volumeToShortUnit(mSma5VolumeList.get(index).getY());
        }

        String vma10 = "";
        if (index >= 0 && index < mSma10VolumeList.size()) {
            vma10 = Utils.volumeToShortUnit(mSma10VolumeList.get(index).getY());
        }

        String vma20 = "";
        if (index >= 0 && index < mSma20VolumeList.size()) {
            vma20 = Utils.volumeToShortUnit(mSma20VolumeList.get(index).getY());
        }

        String vma30 = "";
        if (index >= 0 && index < mSma30VolumeList.size()) {
            vma30 = Utils.volumeToShortUnit(mSma30VolumeList.get(index).getY());
        }

        String vma60 = "";
        if (index >= 0 && index < mSma60VolumeList.size()) {
            vma60 = Utils.volumeToShortUnit(mSma60VolumeList.get(index).getY());
        }

        String str1 = String.format("VOL:%s, " +
                        "<font color='#bbbbbb'>5:%s, </font>" +
                        "<font color='#E7B448'>10:%s, </font>" +
                        "<font color='#FF0000'>20:%s, </font>" +
                        "<font color='#00ff26'>30:%s, </font>" +
                        "<font color='#00D0FC'>60:%s</font>",
                vol, vma5, vma10, vma20, vma30, vma60);


        return  Html.fromHtml(str1);
    }

    @Override
    public String getXAxisLabel(int index) {
        String date = "None";
        if (index >= 0 && index < mXAxisDates.size()) {
            date = mXAxisDates.get(index);
        }
        return date;
    }

    @Override
    public List<String> getXAxisLabelList() {
        return mXAxisDates;
    }

    @Override
    public CombinedData getTaChartData() {
        return mVolumeCombineData;
    }
}
